﻿#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>

typedef struct
{
    int age;
}content;

int main(){
    int shm_id;
    struct shmid_ds shmbuf;
    key_t key;
    key=ftok("shm_test_key",0);
    shm_id = shmget(key,10,IPC_CREAT);
    content *shm_content = (content *)shmat(shm_id,NULL,0);
    int i=0;
    for( i=0; i<10; i++ ){
        printf("%d age:%d\n",getpid(),(*(shm_content+i)).age);
    }
    if(shmdt(shm_content)<0)
        perror("shmdt error");
    if(shmctl(shm_id,IPC_RMID,&shmbuf)<0)
        perror("shmctl error");
    return 0;
}
